﻿
using System;
using System.Collections.Generic;
using UnityEngine;

namespace DL.Tools
{
    /// <summary>
    /// 特效池
    /// </summary>
    public class VFXPoolManager : Singleton<VFXPoolManager>
    {
        /// <summary>
        /// 配置特效
        /// </summary>
        [SerializeField] private List<EffctData> m_EffctDatas;

        //特效池子 所有者_招式表_特效池
        private Dictionary<string, Dictionary<string, ObjectPool<VFXEmitter>>> m_VFXPools;

        protected override void Construct()
        {
            m_VFXPools = new();
            Init();
        }

        //将这些特效先初始化出来
        public void Init()
        {
            foreach (var effct in m_EffctDatas)
            {
                var owner = effct.Owner;
                if (!m_VFXPools.TryGetValue(owner, out var playerVFX))
                {
                    playerVFX = new Dictionary<string, ObjectPool<VFXEmitter>>();
                    m_VFXPools.Add(owner, playerVFX);
                }

                foreach (var item in effct.VFXItems)
                {
                    if (!playerVFX.TryGetValue(item.name, out var pool))
                    {
                        VFXEmitter Create()
                        {
                            var vfx = Instantiate(item.vfxPrefab, item.applyParent ? item.parentTransform : transform,
                                true);
                            vfx.name = item.name;
                            vfx.transform.SetLocalPositionAndRotation(Vector3.zero, item.rotation);
                            vfx.Init(owner, item);
                            return vfx;
                        }

                        void Get(VFXEmitter vfxEmitter)
                        {
                            vfxEmitter.gameObject.SetActive(true);
                        }

                        void ReCycle(VFXEmitter vfxEmitter)
                        {
                            vfxEmitter.transform.SetParent(item.applyParent ? item.parentTransform : transform);
                            vfxEmitter.gameObject.SetActive(false);
                        }

                        void DestroyAct(VFXEmitter vfxEmitter)
                        {
                            vfxEmitter.ReCycle();
                        }

                        pool = new ObjectPool<VFXEmitter>(Create, Get, ReCycle, DestroyAct, true, item.defaultCount,
                            item.maxCount);

                        playerVFX.Add(item.name, pool);
                    }
                }
            }
        }

        //尝试获取一个粒子发生器
        //没有则不会添加新的特效池
        public bool TryGetVFX(string owner, string vfxName, out VFXEmitter vfx, Transform parent = null,
            Vector3 position = default(Vector3), Quaternion quaternion = default(Quaternion))
        {
            vfx = null;
            if (m_VFXPools.TryGetValue(owner, out var playerEffcts))
            {
                if (playerEffcts.TryGetValue(vfxName, out var pool))
                {
                    vfx = pool.Get();
                    vfx.transform.SetParent(parent == null ? transform : parent);
                    vfx.transform.SetLocalPositionAndRotation(position, quaternion);
                    return true;
                }
                else
                {
                    Debugger.EO_LogWarning($"未获取到{owner}下{vfxName}的特效池，请检查参数！");
                }
            }
            else
            {
                Debugger.EO_LogWarning($"未获取到{owner}下的特效分类字典，请检查参数！");
            }

            return false;
        }

        //回收
        public void ReleaseVFX(VFXEmitter vfx)
        {
            if (vfx == null)
            {
                Debugger.EO_LogWarning("回收的vfx为Null");
                return;
            }

            if (m_VFXPools.TryGetValue(vfx.m_Owner, out var playerEffcts))
            {
                if (playerEffcts.TryGetValue(vfx.Config.name, out var pool))
                {
                    pool.Return(vfx);
                }
            }
        }
    }
}